/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package PropertyRental.war.servlets;

import PropertyRental.ejb.persistence.Agent;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;

/**
 *
 * @author Bast
 */
class AddUserAction extends Action {

    public AddUserAction(String success, String fail){
        super(success,fail);
    }

    @Override
    public String perform(HttpServletRequest req) {
        HttpSession session = req.getSession();
        Object account = session.getAttribute("account");
        if (account != null){
            if(account instanceof Agent){
                try{
                    String username = req.getParameter("username");
                    String password = req.getParameter("password");
                    int type = Integer.parseInt(req.getParameter("type"));
                    String givenName = "";
                    String lastName = "";
                    String email = "";
                    double rent = 0;
                    if (type != 1){
                         givenName = req.getParameter("givenname");
                         lastName = req.getParameter("lastname");
                         email = req.getParameter("email");
                         if (type == 3)
                            rent = Double.parseDouble(req.getParameter("rent"));
                    }
                    boolean result = accountEJB.addAccount(username, password, type, givenName, lastName, email, rent);
                    if (result)
                        return this.getSuccesspage();
                    else
                        return this.getFailpage();
                }
                catch (Exception e){
                    return this.getFailpage();
                }
            }
        }
        return this.getFailpage();
    }

}
